from pymemcache.client import base

class CacheUtil:
    mc = ''
    def __init__(self):
        self.mc = base.Client(('localhost', 11212))

    def saveDict(self,parentkey,key,val):
        ret = self.mc.get(parentkey)
        if ret == None:
            dictm = {}
            dictm[key] = val  # 发模式开启debug，会显示错误信息，生成模式时去掉
            self.mc.set(parentkey, dictm)  # memcache只支持键值对形式
        else:
            ret[key] = val
            self.mc.set(parentkey, ret)

    def get(self,key):

        return self.mc.get(key)

    def clear(self):
        self.mc.flush_all()

    def delete(self,key):
        self.mc.delete(key)